package net.zabuchy.activities.action;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import net.zabuchy.activities.entity.HistoryAttribute;
import net.zabuchy.helper.ParamNames;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.issue.bo.IssueBo;
import net.zabuchy.issue.entity.Issue;
import net.zabuchy.user.bo.UserBo;
import net.zabuchy.user.entity.User;

import com.opensymphony.xwork2.Action;
 
public class AssignToUserAction extends ActivityAction implements 
		ServletRequestAware,ServletResponseAware {
 
    private static final Logger LOGGER = Logger.getLogger(AssignToUserAction.class.getName());
 
    @Autowired
    private SessionHelper sessionBean;
    @Autowired
    private IssueBo issueBo;
    @Autowired
	private UserBo userBo;
    
    private HttpServletRequest request;
    private HttpServletResponse response;
    
    private int issueId;
    
    private List<String> usersSystem = new ArrayList<String>(0);
	private String addedUser;
	private Integer userId;
    
    public int getIssueId() {
		return issueId;
	}

	public void setIssueId(int issueId) {
		this.issueId = issueId;
	}

	public String execute() {
    	LOGGER.debug("issueId: " + issueId);
    	LOGGER.debug("addedUser: " + addedUser);
    	List<User> tmp = userBo.findByCustomerId(sessionBean.getCustomerId());
		Iterator it = tmp.iterator();
		while(it.hasNext()) {
			User user = (User) it.next();
			usersSystem.add(user.getUsername());
		}
    	
		Issue issue = issueBo.findById(issueId);
		User user = userBo.findByUsername(addedUser);
		issue.getUsers().add(user);
		user.getIssues().add(issue);
		//issueBo.update(issue);
		userBo.update(user);
		
    	Set<HistoryAttribute> attributes = new HashSet<HistoryAttribute>(0);
    	attributes.add(new HistoryAttribute("addedUser", addedUser, null));
    	saveActivityEvent(issueId, attributes, ParamNames.ASSIGN_TO);
    	
    	return Action.SUCCESS;
    }
    	
	public IssueBo getIssueBo() {
		return issueBo;
	}

	public void setIssueBo(IssueBo issueBo) {
		this.issueBo = issueBo;
	}

	public SessionHelper getSessionBean() {
		return sessionBean;
	}

	public void setSessionBean(SessionHelper sessionBean) {
		this.sessionBean = sessionBean;
	}

	public UserBo getUserBo() {
		return userBo;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	public List<String> getUsersSystem() {
		return usersSystem;
	}

	public void setUsersSystem(List<String> usersSystem) {
		this.usersSystem = usersSystem;
	}

	public String getAddedUser() {
		return addedUser;
	}

	public void setAddedUser(String addedUser) {
		this.addedUser = addedUser;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public void setServletRequest(HttpServletRequest request){
		  this.request = request;
		  }

		  public HttpServletRequest getServletRequest(){
		  return request;
		  }

		  public void setServletResponse(HttpServletResponse response){
		  this.response = response;
		  }

		  public HttpServletResponse getServletResponse(){
		  return response;
		  }

	
}




